//
// Created by kzime on 17-9-4.
//

#include <cstdio>
#include <iostream>
using namespace std;

struct Splay {
    struct Node {
        Node *fa, *ch[2], **root;
        int x, siz, cnt;

        Node(Node **root, Node *fa, int x) : root(root), fa(fa), x(x), cnt(1), siz(1) {
            ch[0] = ch[1] = NULL;
        }

        int relation() {
            return this == fa->ch[1];
        }

        void maintain() {
            siz = cnt;
            if(ch[0]) siz += ch[0]->siz;
            if(ch[1]) siz += ch[1]->siz;
        }

        void rotate() {
            Node *old = fa;
            int r = relation();
            fa = old->fa;
            if(old->fa) old->fa->ch[old->relation()] = this;// 如果有祖父
            if(ch[!r]) ch[!r]->fa = old;
            old->ch[r] = ch[!r];
            old->fa = this;
            ch[!r] = old;
            old->maintain();
            maintain();
            if(!fa) *root = this;
        }

        void splay(Node *target = NULL) { // 如果target缺省, target自动为NULL
            while(fa != target) { 
                if(fa->fa == target) rotate();
                else if(fa->relation() == relation()) {
                    fa->rotate();
                    rotate();
                } else {
                    rotate();
                    rotate();
                }
            }
        }

        Node *pred() {
            Node *v = ch[0];
            while(v->ch[1]) v = v->ch[1];
            return v;
        }

        Node *succ() {
            Node *v = ch[1];
            while(v->ch[0]) v = v->ch[0];
            return v;
        }

        int rank() {
            return ch[0] ? ch[0]->siz : 0;
        } 
    } *root;

    Splay() : root(NULL) {
        insert(0x3f3f3f3f);
        insert(-0x3f3f3f3f);
    }

    Node *insert(int x) {
        // v是指向指针的指针
        Node **v = &root, *fa = NULL;
        while(*v && ((*v)->x ^ x)) {
            fa = *v;
            fa->siz++;
            v = &fa->ch[x >= fa->x];
        }

        if(*v) {
            (*v)->cnt++;
            (*v)->siz++;
        } else (*v) = new Node(&root, fa, x);

        (*v)->splay();
        return root;
    }
   
    Node *find(int x) {
        Node *v = root;
        while(v && (v->x ^ x)) {
            v = v->ch[x >= v->x];
        }
        if(v) v->splay();
        return v;
    }

    void *erase(Node *v) {
        Node *pred = v->pred(), *succ = v->succ();
        pred->splay();
        succ->splay(pred);
        if(v->siz > 1) {
            v->siz--;
            v->cnt--;
        }
        else {
            delete succ->ch[0];
            succ->ch[0] = NULL;
        }
        succ->siz--;
        pred->siz--;
    }

    void erase(int x) {
        Node *v = find(x);
        if(!v) return;
        erase(v);
    }

    int pred(int x) {
        Node *v = find(x);
        if(!v) {
            v = insert(x);
            int res = v->succ()->x;
            erase(v);
            return res;
        }
        else return v->succ()->x;
    } 

    int succ(int x) {
        Node *v = find(x);
        if(!v) {
            v = insert(x);
            int res = v->succ()->x;
            erase(v);
            return res;
        } else return v->succ()->x;
    }

    int rank(int x) {
        Node *v = find(x);
        if(!v) {
            v = insert(x);
            int res = v->rank();
            erase(v);
            return res;
        } else return v->rank();
    }

    int select(int k) {
        Node *v = root;
        while(!(k >= v->rank() && k < v->rank() + v->cnt)) {
            if(k < v->rank()) v = v->ch[0];
            else {
                k -= v->rank() + v->cnt;
                v = v->ch[1];
            }
        }
        v->splay();
        return v->x;
    }

    int min() {
        Node *v = root, *gr = NULL;
        if(!root->ch[0]) return root->succ()->x;
        while(v) {
            gr = v->fa;
            v = v->ch[0];
        }
        return gr->x;
    }

    int max() {
        Node *v = root, *gr = NULL;
        if(!root->ch[1]) return root->pred()->x;
        while(v) {
            gr = v->fa;
            v = v->ch[1];
        }
        return gr->x;
    }

    void print(Node *x) { 
        printf("%d ", x->x);
        if(x->ch[0]) print(x->ch[0]);
        if(x->ch[1]) print(x->ch[1]);
    }
} splay;


int main() { 
#ifndef LOCAL
    freopen("tb_kp.in", "r", stdin);
    freopen("tb_kp.out", "w", stdout);
#endif
    int n;
    scanf("%d", &n);
    while(n--) {
        int op, x;
        scanf("%d", &op);
        if(op < 7) scanf("%d", &x);
        switch(op) {
            case 1:
                splay.insert(x);
                break;
            case 2:
                splay.erase(x);
                break;
            case 3:
                printf("%d\n", splay.rank(x));
                break;
            case 4:
                printf("%d\n", splay.select(x));
                break;
            case 5:
                printf("%d\n", splay.pred(x));
                break;
            case 6:
                printf("%d\n", splay.succ(x));
                break;
            case 7:
                printf("%d\n", splay.min());
                break;
            case 8:
                printf("%d\n", splay.max());
        }
        //splay.print(splay.root);
        //putchar(' ');
    }
}
